package com.nine.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nine.entity.MyReturn;
import com.nine.entity.PageSort;
import com.nine.entity.TestPlan;
import com.nine.entity.Userinfo;
import com.nine.service.impl.TestPlanServiceImpl;
import com.nine.service.impl.TesterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.SimpleTimeZone;

/**
 * @Description:TestPlan的控制层
 * @Author:LouFangRui
 * @Date:2022/7/1 10:15
 */
@RestController
@RequestMapping("testPlan")
public class TestPlanController extends ControllerFather {

	@Autowired
	private TestPlanServiceImpl testPlanService;

	MyReturn myReturn = new MyReturn();

	/**
	 * 查询全部and模糊查询 and 分页查询
	 */
	@RequestMapping("selectAllPlan.do")
	public MyReturn selectAllPlan(PageSort pageSort, TestPlan testPlan) {
		Page page=testPlanService.selectAllPlanByPage(pageSort,testPlan);
		//获取分页之后的数据并向前端传递
		List pageResult = page.getResult();
		myReturn.setData(pageResult);
		//获取总数据条数并向前端传递
		long total = page.getTotal();
		myReturn.setCount(total);

		return super.getSuccessResult(myReturn);
	}

	/**
	 * 添加
	 */
	@RequestMapping("addTestPlan.do")
	public MyReturn addTestPlan(TestPlan testPlan, HttpSession httpSession) {

		//获取session中登录者的姓名，并放入实体内为创建人赋值
		Userinfo userInfo = (Userinfo) httpSession.getAttribute("userInfo");
		String userName = userInfo.getUserName();
		testPlan.setCreateBy(userName);

		//获取当前时间为实体的创建时间赋值
		testPlan.setCreateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));

		//初始化变量
		StringBuffer testCode1 = new StringBuffer();
		int num ;
		//生成随机数
		for (int i = 0; i < 6; i++) {
			num= (int) (Math.random()*10);
			testCode1.append(num);
		}
		//把生成的随机数放入实体中作为验证码使用
		testPlan.setTestCode(Integer.valueOf(String.valueOf(testCode1)));

		System.out.println(testPlan.toString());

		int i = testPlanService.addTestPlan(testPlan);
		if (i > 0) {
			return super.getSuccessResult(myReturn);
		} else {
			return super.getFailResult(myReturn);
		}
	}

	/**
	 * 修改
	 */
	@RequestMapping("updatePlanById.do")
	public MyReturn updatePlanById(TestPlan testPlan,HttpSession httpSession) {

		//获取session中登录者的姓名，并放入实体内为修改人赋值
		Userinfo userInfo = (Userinfo) httpSession.getAttribute("userInfo");
		String userName = userInfo.getUserName();
		testPlan.setUpdateBy(userName);

		//获取当前时间为实体的修改时间赋值
		testPlan.setUpdateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())));


		int i = testPlanService.updatePlanById(testPlan);
		if (i > 0) {
			return super.getSuccessResult(myReturn);
		} else {
			return super.getFailResult(myReturn);
		}
	}

	/**
	 * 行内删除and批量删除
	 */
	@RequestMapping("deletePlanById.do")
	public MyReturn deletePlanById(String[] testPlanId) {
		int i = testPlanService.deletePlanById(testPlanId);
		if (i > 0) {
			return super.getSuccessResult(myReturn);
		} else {
			return super.getFailResult(myReturn);
		}

	}

}
